using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using secret_server.Data;
using secret_server.Models;
using Microsoft.AspNetCore.Authorization;

namespace secret_server.Controllers
{
    [Authorize]
    public class CouplesController : Controller
    {
        private readonly SecretDbContext _context;

        public CouplesController(SecretDbContext context)
        {
            _context = context;
        }

        // GET: Couples
        public async Task<IActionResult> Index()
        {
            var secretDbContext = _context.Couples.Include(c => c.AiCharacterScene).Include(c => c.CurrentTopic).Include(c => c.ManualCharacterScene);
            return View(await secretDbContext.ToListAsync());
        }

        // GET: Couples/Details/5
        public async Task<IActionResult> Details(Guid? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var couple = await _context.Couples
                .Include(c => c.AiCharacterScene)
                .Include(c => c.CurrentTopic)
                .Include(c => c.ManualCharacterScene)
                .SingleOrDefaultAsync(m => m.Id == id);
            if (couple == null)
            {
                return NotFound();
            }

            return View(couple);
        }

        // GET: Couples/Create
        public IActionResult Create()
        {
            ViewData["AiCharacterSceneId"] = new SelectList(_context.CharacterScenes, "Id", "AVUser");
            ViewData["CurrentTopicId"] = new SelectList(_context.Topices, "Id", "FirstQuestion");
            ViewData["ManualCharacterSceneId"] = new SelectList(_context.CharacterScenes, "Id", "AVUser");
            return View();
        }

        // POST: Couples/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Status,WorldSceneId,ManualCharacterSceneId,AiCharacterSceneId,ConversationId,Break,Marry,Fucker,CurrentTopicId")] Couple couple)
        {
            if (ModelState.IsValid)
            {
                couple.Id = Guid.NewGuid();
                _context.Add(couple);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            ViewData["AiCharacterSceneId"] = new SelectList(_context.CharacterScenes, "Id", "AVUser", couple.AiCharacterSceneId);
            ViewData["CurrentTopicId"] = new SelectList(_context.Topices, "Id", "FirstQuestion", couple.CurrentTopicId);
            ViewData["ManualCharacterSceneId"] = new SelectList(_context.CharacterScenes, "Id", "AVUser", couple.ManualCharacterSceneId);
            return View(couple);
        }

        // GET: Couples/Edit/5
        public async Task<IActionResult> Edit(Guid? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var couple = await _context.Couples.SingleOrDefaultAsync(m => m.Id == id);
            if (couple == null)
            {
                return NotFound();
            }
            ViewData["AiCharacterSceneId"] = new SelectList(_context.CharacterScenes, "Id", "AVUser", couple.AiCharacterSceneId);
            ViewData["CurrentTopicId"] = new SelectList(_context.Topices, "Id", "FirstQuestion", couple.CurrentTopicId);
            ViewData["ManualCharacterSceneId"] = new SelectList(_context.CharacterScenes, "Id", "AVUser", couple.ManualCharacterSceneId);
            return View(couple);
        }

        // POST: Couples/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(Guid id, [Bind("Id,Status,WorldSceneId,ManualCharacterSceneId,AiCharacterSceneId,ConversationId,Break,Marry,Fucker,CurrentTopicId")] Couple couple)
        {
            if (id != couple.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(couple);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CoupleExists(couple.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            ViewData["AiCharacterSceneId"] = new SelectList(_context.CharacterScenes, "Id", "AVUser", couple.AiCharacterSceneId);
            ViewData["CurrentTopicId"] = new SelectList(_context.Topices, "Id", "FirstQuestion", couple.CurrentTopicId);
            ViewData["ManualCharacterSceneId"] = new SelectList(_context.CharacterScenes, "Id", "AVUser", couple.ManualCharacterSceneId);
            return View(couple);
        }

        // GET: Couples/Delete/5
        public async Task<IActionResult> Delete(Guid? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var couple = await _context.Couples
                .Include(c => c.AiCharacterScene)
                .Include(c => c.CurrentTopic)
                .Include(c => c.ManualCharacterScene)
                .SingleOrDefaultAsync(m => m.Id == id);
            if (couple == null)
            {
                return NotFound();
            }

            return View(couple);
        }

        // POST: Couples/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(Guid id)
        {
            var couple = await _context.Couples.SingleOrDefaultAsync(m => m.Id == id);
            _context.Couples.Remove(couple);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool CoupleExists(Guid id)
        {
            return _context.Couples.Any(e => e.Id == id);
        }
    }
}
